/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.task.nlp.japanese;

import java.io.File;

import edu.cmu.lti.sepia.util.WrapperUtil;

public class NLPTool {
	
	public File mecabBin = new File( "/home/javelin/extern/cabocha-pack/bin/mecab" );
	public String mecabEnc = "euc-jp";
	
	public NLPTool() throws Exception {
		
		//mecabBin = new File( p.getProperty( "mecabBin" ).trim() );
		//mecabEnc = p.getProperty( "mecabEnc", "euc-jp" ).trim();
		
		if ( ! mecabBin.exists() ) {
			System.err.println("NLPTool Make sure following path exists: "
					+ mecabBin.getAbsolutePath()+"\n");
		}
	}
	
	public NLPTool(String mecabBin, String mecabEnc) throws Exception {
		this.mecabBin = new File(mecabBin);
		this.mecabEnc  = mecabEnc;
		if ( ! this.mecabBin.exists() ) {
			throw new Exception("Make sure following path exists: "
					+ this.mecabBin.getAbsolutePath()+"\n");
		}
	}
	
	/**
	 * Segmentation tool for Japanese (by MeCab)
	 * @param Japanese sentence
	 * @return segmented sentence with ASCII white space
	 */
	public String segment(String japaneseText) {
		String command = mecabBin.getAbsolutePath()+" --output-format-type=wakati";
		String encoding = mecabEnc;
		String segmentedWord = WrapperUtil.execute(command, japaneseText, encoding).trim();
		segmentedWord = HankakuToZenkakuConverter.convert(segmentedWord);
		segmentedWord = NumberUtil.reformatNumber(segmentedWord);
		return segmentedWord;
	}
	
	public String runMecab(String text, int nBest) {
		String command = mecabBin.getAbsolutePath()+" -N"+nBest;
		String encoding = mecabEnc;
		return WrapperUtil.execute(command, text, encoding).trim();
	}
	
	public String runMecab(String text) {
		return runMecab( text, 1 );
	}
	
	public String removeIllegalChars(String text) {
    	if ( text == null || text.length()==0 ) return "";
    	return text.replaceAll("[\\.,?!+_*&%\\$#@\\\\~\\-\\(\\)*\\}\\{\\]\\[\\^\"\':;]", "");
    }
		
}
